package me.stone.stanimeclient.page.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.RecyclerView.RecycledViewPool
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.scopes.ActivityScoped
import kotlinx.coroutines.launch
import me.stone.stanimeclient.MainViewModel
import me.stone.stanimeclient.core.common.extensions.detachParent
import me.stone.stanimeclient.core.common.extensions.launchAndRepeatWithLifecycle
import me.stone.stanimeclient.core.ui.adapters.FanDailyUpdateAdapter
import me.stone.stanimeclient.databinding.FragmentHomeUpdateBinding
import javax.inject.Inject
import javax.inject.Qualifier

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class DailyUpdateViewPool

@Module
@InstallIn(ActivityComponent::class)
object RecycledPoolModules {

    @ActivityScoped
    @Provides
    @DailyUpdateViewPool
    fun providesDailyUpdatePool(): RecycledViewPool = RecycledViewPool()

}

@AndroidEntryPoint
class HomeUpdateFragment: Fragment() {

    @Inject
    lateinit var adapter: FanDailyUpdateAdapter

    @Inject
    @DailyUpdateViewPool
    lateinit var recycledViewPool: RecycledViewPool

    private val mainViewModel: MainViewModel by activityViewModels()

    private val viewModel: HomeViewModel by activityViewModels()

    private var binding: FragmentHomeUpdateBinding? = null

    private var weekday: Int = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        weekday = savedInstanceState?.getInt(KEY_WEEK) ?: weekday
        if (binding == null) {
            binding = FragmentHomeUpdateBinding.inflate(
                inflater, container, false
            ).apply {
                updateList.adapter = adapter
                updateList.setRecycledViewPool(recycledViewPool)
            }
            showProgress(true)
        } else {
            binding!!.root.detachParent()
        }
        return binding!!.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter.setOnItemClickListener { item ->
            mainViewModel.seeFanDetailItem(item.id)
        }
        launchAndRepeatWithLifecycle {
            launch {
                viewModel.dailyData.collect {
                    showProgress(true)
                    adapter.submitList(it[weekday])
                    showProgress(false, adapter.currentList.isEmpty())
                }
            }
        }
    }

    private fun showProgress(show: Boolean, empty: Boolean = false) = binding?.apply {
        loadContent.text = if (empty) "没有找到更多数据~" else "正在努力加载数据..."
        groupProgress.visibility = if (show || empty) View.VISIBLE else View.GONE
        updateList.visibility = if (show || empty) View.GONE else View.VISIBLE
    }

    fun setWeekday(weekday: Int) {
        this.weekday = weekday
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt(KEY_WEEK, weekday)
    }

    companion object {
        private const val KEY_WEEK = "FRAGMENT_WEEK"
    }

}